//
//  ICMShowImageView.m
//  ICrEAM
//
//  Created by rainimute on 16/10/31.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMShowImageView.h"

@interface ICMShowImageView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *blackBoard;

@property (nonatomic, strong) NSMutableArray *imageViews;

@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation ICMShowImageView
#pragma mark    初始化
- (instancetype)initWithImages:(NSMutableArray*)images currentIndex:(NSInteger)currentIndex IsPreview:(BOOL)isPreview{
    if (self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]) {
        self.backgroundColor = [UIColor colorWithHex:0x000000 andAlpha:0.2];
        //
        self.imageViews = [NSMutableArray array];
        
        self.blackBoard = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 170, ScreenWidth, ScreenHeight-240)];
        UITapGestureRecognizer *backgroundTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backgroundTapAction:)];
        backgroundTap.numberOfTapsRequired = 1;
        [self.blackBoard addGestureRecognizer:backgroundTap];
        
        self.blackBoard.backgroundColor = [UIColor clearColor];
        self.blackBoard.showsHorizontalScrollIndicator = NO;
        self.blackBoard.bounces = NO;
        self.blackBoard.delegate = self;
        self.blackBoard.scrollEnabled = YES;
        self.blackBoard.pagingEnabled = YES;
        for (NSInteger i=0; i<images.count; i++) {
            UIScrollView *blackBoard = [[UIScrollView alloc]initWithFrame:CGRectMake(i*ScreenWidth, 0, ScreenWidth, ScreenHeight-240)];
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-240)];
            imageView.userInteractionEnabled = YES;
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.image = images[i];
//            if(!isPreview)
//            {
//                imageView.image = images[i];
//            }
//            else
//            {
//                //设置网络图片
//                NSString *imageName = [NSString stringWithFormat:@"getcar_empty_image_%@",[ICrEAMTool isVW]?@"vw":@"sk"];
//                [imageView sd_setImageWithURL:[NSURL URLWithString:images[i]] placeholderImage:[UIImage imageNamed:imageName]];
//            }
            [self.imageViews addObject:imageView];
            blackBoard.backgroundColor = [UIColor clearColor];
            blackBoard.showsHorizontalScrollIndicator = NO;
            blackBoard.bounces = NO;
            blackBoard.delegate = self;
            blackBoard.scrollEnabled = YES;
            blackBoard.maximumZoomScale = 2.0;
            blackBoard.minimumZoomScale = 1.0;
            blackBoard.contentSize = CGSizeMake(ScreenWidth, ScreenHeight-240);
            [blackBoard addSubview:imageView];
            [self.blackBoard addSubview:blackBoard];
        }
        self.blackBoard.contentSize = CGSizeMake(images.count*ScreenWidth, ScreenHeight-240);
        self.blackBoard.contentOffset = CGPointMake(currentIndex*ScreenWidth, 0);
        [self addSubview:self.blackBoard];
        
        self.currentIndex = currentIndex;
        
        [Window addSubview:self];
    }
    return self;
}
#pragma mark    背景点击
- (void)backgroundTapAction:(UITapGestureRecognizer*)tap{
    [self removeFromSuperview];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.blackBoard) {
        self.currentIndex = scrollView.contentOffset.x/ScreenWidth;
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    UIImageView *imageView = self.imageViews[self.currentIndex];
    return imageView;
}

+ (instancetype)createShowImageViewWithImage:(NSMutableArray *)images currentIndex:(NSInteger)currentIndex IsPreview:(BOOL)isPreview{
    return [[ICMShowImageView alloc]initWithImages:images currentIndex:currentIndex IsPreview:isPreview];
}

@end
